package com.woniuxy.hrp.service.Serviceimpl;

import com.woniuxy.hrp.entity.*;
import com.woniuxy.hrp.mapper.DoctorAndTimeMapper;
import com.woniuxy.hrp.mapper.PayRecordMapper;
import com.woniuxy.hrp.mapper.ReservationMapper;
import com.woniuxy.hrp.mapper.WalletMapper;
import com.woniuxy.hrp.service.DoctorService;
import com.woniuxy.hrp.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 李国杰
 * @Description //TODO
 * @Date 10:07 2022/8/30
 **/
@Service
public class ReservationServiceImp implements ReservationService {
    @Autowired
    ReservationMapper reservationMapper;
    @Autowired
    PayRecordMapper payRecordMapper;
    @Autowired
    WalletMapper walletMapper;

    @Autowired
    private DoctorService doctorService;
    @Autowired
    private DoctorAndTimeMapper doctorAndTimeMapper;


    @Override
    public int addReservation(Reservation reservation, PreOv preOv) {
        /**
         * @Author 李国杰
         * @Description //参数中必须要有doctorandtime_id
         * @Date 10:08 2022/8/30
         * @Param [doctor_id, patient_id]
         * @return void
         **/
        //判断是否可以约，可以就加记录进去
        int  doctorandtime= doctorService.preAppoint(preOv.getDoctorid(),preOv.getDate());
        System.out.println("返回的值为"+doctorandtime);
        if (doctorandtime>0) {
            DoctorAndTime doctorAndTime=new DoctorAndTime();
            doctorAndTime.setId(doctorandtime);
            reservation.setDoctorAndTime(doctorAndTime);
            reservationMapper.add(reservation);
            return doctorandtime;
        }else{
            return doctorandtime;
        }
    }

    @Override
    public void deleteReservationById(int id) {
        Reservation reservation = reservationMapper.selectById(id);
        if (reservation != null) {
            //根据预约id
            PayRecord payRecord = payRecordMapper.selectByReservationId(reservation.getId());
            //将该支付记录，根据支付记录将钱退回到钱包
            Wallet wallet = new Wallet();
            wallet.setBalance(payRecord.getFee());
            walletMapper.update(wallet);
            //将该预约信息进行修改,修改为已退款
            reservation.setStatus("r");
            reservationMapper.update(reservation);
        }

    }

    @Override
    public List<Reservation> seleReservationByCon(Reservation reservation) {
        return reservationMapper.selectByCondition(reservation);
    }

    @Override
    public List<Reservation> selectBypidandstatus(int id, String status) {
        return reservationMapper.selectBypidandstatus(id,status);
    }

    @Override
    public Reservation selectbypidanddtid(String status, int doctor_timeid) {
        return reservationMapper.selectbypidanddtid(status,doctor_timeid);
    }

    @Override
    public void update(Reservation reservation) {
        reservationMapper.update1(reservation);
    }
}
